package com.xiaochangwei.web.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @since 2016年11月23日 下午2:01:35
 * @author 肖昌伟 317409898@qq.com
 * @description
 */
public class DateUtil {
	public static final String YYYYMM = "yyyyMM";

	public static final String YYYYMMDD = "yyyyMMdd";

	public static final String YYYYMMDD_ = "yyyy-MM-dd";

	public static final String yyyyMMddHHmmss = "yyyyMMddHHmmss";

	public static final String yyyyMMddHHmmss_ = "yyyy-MM-dd HH:mm:ss";

	public static final String yyyyMMddHHmmssSSS = "yyyyMMddHHmmssSSS";

	public static final String yyyyMdHms_ = "yyyy-M-d H:m:s";

	public static final String yyMdHms = "yy-M-d H:m:s";

	private static final int DAY = 86400000;

	private static final int HOURS = 3600000;

	private static final int MINUTES = 60000;

	private static final int SECONDS = 1000;

	public static Date stringToDate(String pattern, String date) throws Exception {
		SimpleDateFormat format = new SimpleDateFormat(pattern);
		try {
			return format.parse(date);
		} catch (ParseException e) {
			return null;
		}

	}

	/**
	 * 日期转换为字符串
	 * 
	 * @param pattern
	 * @param date
	 * @return
	 */
	public static String dateToString(String pattern, Date date) {
		SimpleDateFormat format = new SimpleDateFormat(pattern);
		return format.format(date);
	}

	/**
	 * 计算两个时间相差几小时
	 * 
	 * @param beginDate
	 * @param endDate
	 * @return
	 */
	public static Double timeDiff(Date beginDate, Date endDate) {
		double hours = (double) (endDate.getTime() - beginDate.getTime()) / 3600 / 1000;
		return hours;
	}

	/**
	 * 计算两个时间相差几分钟
	 * 
	 * @param beginDate
	 * @param endDate
	 * @return
	 */

	public static int timeDiff(String beginDate, Date endDate) throws Exception {
		Date bDate = DateUtil.stringToDate(yyyyMMddHHmmss_, beginDate);
		int minutes = (int) ((endDate.getTime() - bDate.getTime()) / 60 / 1000);
		return minutes;
	}

	/**
	 * 计算两个时间相差秒钟数
	 * 
	 * @param beginDate
	 * @param endDate
	 * @return
	 */

	public static int timeDiffBySecond(String beginDate, String endDate) throws Exception {
		Date aDate = DateUtil.stringToDate(yyyyMMddHHmmss_, endDate);
		Date bDate = DateUtil.stringToDate(yyyyMMddHHmmss_, beginDate);
		int totalSecond = (int) ((aDate.getTime() - bDate.getTime()) / 1000);
		return totalSecond;
	}

	public static int timeDiffBySecond(Date beginDate, Date endDate) throws Exception {
		int totalSecond = (int) ((endDate.getTime() - beginDate.getTime()) / 1000);
		return totalSecond;
	}

	public static String time2Str(long time) {
		StringBuilder buff = new StringBuilder(32);
		long leftTime = time;
		long count = 0;
		if (leftTime > DAY) {
			count = leftTime / DAY;
			leftTime = leftTime - (count * DAY);
			buff.append(count + "天");
		}
		if (leftTime > HOURS) {
			count = leftTime / HOURS;
			leftTime = leftTime - (count * HOURS);
			buff.append(count + "小时");
		}
		if (leftTime > MINUTES) {
			count = leftTime / MINUTES;
			leftTime = leftTime - (count * MINUTES);
			buff.append(count + "分");
		}
		if (leftTime > SECONDS) {
			count = leftTime / SECONDS;
			buff.append(count + "秒");
		}
		return buff.toString();
	}

	public static String minute2DHS(Long minute) {
		StringBuffer buff = new StringBuffer(32);
		long leftTime = minute;
		long count = 0;
		if (leftTime > 60 * 24) {
			count = leftTime / (60 * 24);
			leftTime = leftTime - (count * (60 * 24));
			buff.append(count + "天");
		}
		if (leftTime > 60) {
			count = leftTime / 60;
			leftTime = leftTime - (count * 60);
			buff.append(count + "小时");
		}
		if (leftTime >= 0) {
			buff.append(leftTime + "分钟");
		}
		return buff.toString();
	}

	/**
	 * 两个日期相差天数，严格按照时分秒计算
	 * 
	 * @param fDate
	 *            开始日期
	 * @param oDate
	 *            结束日期
	 * @return
	 */
	public static int getIntervalDays(Date fDate, Date oDate) {
		if (null == fDate || null == oDate) {
			return -1;
		}
		long intervalMilli = oDate.getTime() - fDate.getTime();
		return (int) (intervalMilli / (24 * 60 * 60 * 1000));
	}

	/**
	 * 两个日期相差天数，仅按照日期计算
	 * 
	 * @param fDate
	 *            开始日期
	 * @param oDate
	 *            结束日期
	 * @return
	 */
	public static int daysOfTwo(Date fDate, Date oDate) {
		Calendar aCalendar = Calendar.getInstance();
		aCalendar.setTime(fDate);
		int day1 = aCalendar.get(Calendar.DAY_OF_YEAR);
		aCalendar.setTime(oDate);
		int day2 = aCalendar.get(Calendar.DAY_OF_YEAR);
		return day2 - day1;
	}

}
